<?php
/**
 * @since      File available since Release v1.1
 */

class categoryControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
        
	}
	
	/*
	 * 发现首页
	 */
	public function indexOp(){
		
		$model = Model();
		$listparent = $model->table('category')->where(array('parent_id'=>'0'))->order('categorysort asc')->select();
		
		if(!empty($listparent)){
			foreach($listparent as $key=>$val){
				$listparent[$key]['son'] = $model->table('category')->where(array('parent_id'=>$val['id']))->order('categorysort asc')->select();
			}
		}		
		Tpl::output('listparent',$listparent);
		
		$condition = array();
		$condition['categoryrecommend'] = 1;
		$condition['parent_id'] = array('neq','0');
		$recommendcategory = $model->table('category')->where($condition)->order('categorysort asc')->select();
		Tpl::output('recommendcategory',$recommendcategory);

		Tpl::showpage('category.index');
	}
	
	
	public function categorylistOp(){
		
		$template_id = $_COOKIE['template_id'];		$sort=$_GET['fieldsort'];		Tpl::output('sort',$sort);
		if($template_id==1){
			Tpl::showpage('category.list1');
		}else{
			Tpl::showpage('category.list0');
		}
	}
	
	
	public function categorydataOp(){
		
		$condition = array();
		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = 1;
		$condition['is_state'] = '0';
		$condition['yikoujia_end'] = '0';
		if(!empty($_GET['cate_0'])){
			$condition['cate_id'] = intval($_GET['cate_0']);
		}
		
		if(!empty($_GET['cate_1'])){
			$condition['cate_id_1'] = intval($_GET['cate_1']);
		}
		
		if(in_array($_GET['fieldsort'],array('auction_id','end_time'))){
			if($_GET['fieldsort'] == 'auction.id'){
				$ordersort = "auction_id desc";
			}elseif($_GET['fieldsort'] == 'end_time'){
				$ordersort = "end_time asc";
			}
		}else{
			$ordersort = "is_jingxuan desc";
		}
		
		$nextpage = intval($_GET['nextpage']);
		$nextnum = ($nextpage-1)*6;
		$limit = "{$nextnum},6";
		
		$model = Model();
		$field = 'auction.*,user.avatar as avatar';
		$list = $model->field($field)->table('auction,user')->join('left')->on('auction.seller_id = user.user_id')->where($condition)->limit($limit)->order($ordersort)->select();
		
		if(empty($list)) exit;
		Tpl::output('list',$list);
		$template_id = $_COOKIE['template_id'];
		if($template_id==1){
			Tpl::showpage('category.data1');
		}else{
			Tpl::showpage('category.data0');
		}

	}

	
	public function settemplateOp(){
		
		$template_id = intval($_GET['template_id']);
		
		setcookie('template_id',$template_id,time()+3600*24*100);
		
		echo json_encode(array('state'=>1));
		exit;
	}
	
	
}